import time

import allure
import requests

import app


class UserManageApi:
    def __init__(self, ip_port="http://ihrm2-test.itheima.net"):
        self.ip_port = ip_port

    # 添加员工
    @allure.step(title="新增user")
    def add_user(self, headers, username=None, mobile=None, workNumber=None, path="/api/sys/user", timeOfEntry=None,
                 formOfEmployment=1, departmentId=None, correctionTime=None, body=None):
        # 定义 添加员工url
        add_user_url = self.ip_port + path
        allure.attach('URL', add_user_url)
        allure.attach('headers', str(headers))
        app.HEADERS = headers
        if body is None:
            body = {
                "username": username,
                "mobile": mobile,
                "timeOfEntry": timeOfEntry,
                "formOfEmployment": formOfEmployment,
                "workNumber": workNumber,
                "departmentId": departmentId,
                "correctionTime": correctionTime
            }

        allure.attach('request.body', str(body))
        # 添加员工

        response = requests.post(add_user_url,
                                 json=body,
                                 headers=headers)

        allure.attach('response.status_code', str(response.status_code))
        allure.attach('response.body', response.text)
        return response

    def get_user(self, user_id, headers, path="/api/sys/user"):
        # 定义查询员工的url
        get_user_url = self.ip_port + path + "/" + user_id

        # 发送查询员工请求
        response = requests.get(get_user_url, headers=headers)
        return response

    def put_user(self, user_id, headers, username, path="/api/sys/user"):
        # 定义修改员工的url
        put_user_url = self.ip_port + path + "/" + user_id

        # 发送修改员工请求
        response = requests.put(url=put_user_url,
                                json={"username": username},
                                headers=headers)
        return response

    @allure.step(title="删除user")
    def delete_user(self, user_id, headers, path="/api/sys/user"):
        # 定义删除员工的URL
        delete_user_url = self.ip_port + path + "/" + user_id

        allure.attach('URL', delete_user_url)
        allure.attach('headers', str(headers))
        app.HEADERS = headers

        # 发送删除员工的请求
        response = requests.delete(url=delete_user_url, headers=headers)

        allure.attach('response.status_code', str(response.status_code))
        allure.attach('response.body', response.text)
        return response
